package com.getinstaliker.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.getinstaliker.screen.GetCoinsFragment;
import com.getinstaliker.screen.GetLikesFragment;
import com.getinstaliker.screen.MoreFragment;
import com.getinstaliker.screen.SpendFragment;
import com.getinstaliker.util.UniversalImageLoader;
import com.getinstaliker.util.network.util.AndroidVersionUtils;
import com.instagram.getinstaliker.R;

// TODO: Auto-generated Javadoc
/**
 * The Class MainFragmentActivity.
 * @author HuanND
 */
public class MainFragmentActivity extends BaseFragmentActivity {

	/** The m tab host. */
	private FragmentTabHost mTabHost;
	
	/** The screen width. */
	public static int SCREEN_WIDTH;
	
	/** The screen height. */
	public static int SCREEN_HEIGHT;

	/* (non-Javadoc)
	 * @see com.getinstaliker.activity.BaseFragmentActivity#onCreate(android.os.Bundle)
	 */
	/**
	 * On create.
	 *
	 * @param savedInstanceState the saved instance state
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initScreenSize();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		prepareForApp();
		initTab();
	}
	
	/**
	 * Prepare for app.
	 */
	private void prepareForApp(){
		
	}

	/**
	 * Inits the tab.
	 */
	@SuppressLint("NewApi")
	private void initTab() {
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		if(AndroidVersionUtils.isHoneycombOrHigher()){
			mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
		}else{
			mTabHost.getTabWidget().setDividerDrawable(null);
		}
		
		mTabHost.addTab(mTabHost.newTabSpec("getcoins").setIndicator(getIndicator(getString(R.string.get_coins), R.drawable.selector_ico_tab_get_coins)),
				GetCoinsFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("getlikes").setIndicator(getIndicator(getString(R.string.get_likes), R.drawable.selector_ico_tab_get_likes)), 
				GetLikesFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("more").setIndicator(getIndicator(getString(R.string.more), R.drawable.selector_ico_tab_more)),
				MoreFragment.class, null);
//		mTabHost.getTabWidget().setStripEnabled(false);
		// setContentView(mTabHost);
	}
	
	/**
	 * Gets the indicator.
	 *
	 * @param text the text
	 * @param drawableId the drawable id
	 * @return the indicator
	 */
	public View getIndicator(String text,int drawableId){
		View v = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
		if(text!=null){
			((TextView)v.findViewById(R.id.tvTitle)).setText(text);
		}
		if(drawableId!=0){
			((ImageView)v.findViewById(R.id.imgTabIcon)).setImageResource(drawableId);
		}
		return v;
	}

	/**
	 * Inits the screen size.
	 */
	private void initScreenSize() {
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		SCREEN_HEIGHT = displaymetrics.heightPixels;
		SCREEN_WIDTH = displaymetrics.widthPixels;
	}
	
	/* (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onDestroy()
	 */
	/**
	 * On destroy.
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		UniversalImageLoader.getInstance().clearMemoryCache();
		UniversalImageLoader.getInstance().clearDiscCache();
	}

}
